/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.ActorJpaController;
import DAO.ArtistJpaController;
import DAO.DirectorJpaController;
import DAO.PersonJpaController;
import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import Entidad.Actor;
import Entidad.Artist;
import Entidad.Director;
import Entidad.Person;
import grlu.Main;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PersonControl {
    private ActorJpaController JPAActor;
    private ArtistJpaController JPAArtist;
    private DirectorJpaController JPADirector;
    private PersonJpaController JPAPerson;
    private String aux;
    
    public PersonControl(){
        JPAActor = new ActorJpaController(Main.emf);
        JPAArtist = new ArtistJpaController(Main.emf);
        JPADirector = new DirectorJpaController(Main.emf);
        JPAPerson = new PersonJpaController(Main.emf);
        aux = "";
    }
    
    public int createActor(Actor actor){
        setUnknowns((Person)actor);
        try {
            JPAActor.create(actor);
        } catch (PreexistingEntityException ex) {
            return 2;
        } catch (Exception ex) {
            return 1;
        }
        return 0;
    }
    
    public int createArtist(Artist artist){
        setUnknowns((Person)artist);
        try {
            JPAArtist.create(artist);
        } catch (PreexistingEntityException ex) {
            return 2;
        } catch (Exception ex) {
            return 1;
        }
        return 0;
    }
    
    public int createDirector(Director director){
        setUnknowns((Person)director);
        try {
            JPADirector.create(director);
        } catch (PreexistingEntityException ex) {
            return 2;
        } catch (Exception ex) {
            return 1;
        }
        return 0;
    }
    
    public Person findPerson(String name){
        return JPAPerson.findPerson(name);
    }
    
    public int deletePerson(Person person){
        try {
            JPAPerson.destroy(person.getName());
        } catch (NonexistentEntityException ex) {
            return 1;
        }
        return 0;
    }
    
    public void setUnknowns(Person person){
        aux = (person.getLastName() == null)? "Unknown" : person.getLastName();
        person.setLastName(aux);
        aux = (person.getBirthPlace() == null)? "Unknown" : person.getBirthPlace();
        person.setBirthPlace(aux);
        aux = (person.getPicture() == null)? "Unknown" : person.getPicture();
        person.setPicture(aux);
        java.util.Date birth;
        birth = (person.getBirthDate() == null)? new java.util.Date(0) : person.getBirthDate();
        person.setBirthDate(birth);
    }
}
